/**
 * Created by Administrator on 2015/5/6.
 */
define(['jquery', 'art-dialog'], function ($) {
    var stieurl = location.href.slice(0, 8) + location.href.slice(8, location.href.length).split('/')[0];
    $.ajaxSetup({
        timeout: 600//60秒超时
    });

    var getEventTarget = function(args){
        var event = args.callee.caller.arguments[0];
        return event.srcElement || event.target;
    };
//========用户相关==========//
    $.User = {
        /**
         * @param uNameSelector 用户名选择器
         * @param pwdSelector   密码选择器
         * @param verifyCodeSelector  验证码选择器
         */
        login: function (uNameSelector, pwdSelector, verifyCodeSelector) {
            if (!$.User.checkInput(uNameSelector, pwdSelector)) {
                return;
            }
            var username = $(uNameSelector).val(),
                password = $(pwdSelector).val();
            var btn = getEventTarget(arguments);
            var $btn = $(btn).button('loading');
            $.post('/user/login', {username: username, password: password}, function (ret) {
                if (ret.ret === -1) {
                    dialog({
                        padding: 10,
                        title: '错误',
                        content: '用户名或密码错误登录失败',
                        okValue: '确定',
                        ok: $.noop
                    }).show($(uNameSelector).get(0));
                } else {
                    //console.log(stieurl);
                    location.href = stieurl;
                }
                $btn.button('reset');
            }).done(function(){
                $.User.formReset();
            });
        },
        /**
         * @param uNameSelector 用户名选择器
         * @param pwdSelector   密码选择器
         * @param verifyCodeSelector  验证码选择器
         */
        register: function (uNameSelector, pwdSelector, verifyCodeSelector) {
            if (!$.User.checkInput(uNameSelector, pwdSelector)) {
                return;
            }
            var username = $(uNameSelector).val(),
                password = $(pwdSelector).val(),
                verifyCode = $(verifyCodeSelector).val();
            var event = arguments.callee.caller.arguments[0];
            var btn = event.srcElement || event.target;
            var $btn = $(btn).button('loading');
            $.post('/user/register', {username: username, password: password, verifyCode: verifyCode}, function (ret) {
                var options = {
                    padding: 10,
                    title: '提示',
                    okValue: '确定'
                };
                if (ret.ret === -1) {
                    options.content = '<font color="red">注册失败</font>';
                } else {
                    options.content = '<font color="green">注册成功</font>';
                    options.ok = function () {
                        location.href = stieurl + '/page/login';
                    };
                }
                dialog(options).showModal();
                $btn.button('reset');
            });
        },
        /**
         * 检查用户名/密码输入是否合法
         * @param uNameSelector
         * @param pwdSelector
         * @returns {boolean}
         */
        checkInput: function (uNameSelector, pwdSelector) {
            var username = $(uNameSelector).val(),
                password = $(pwdSelector).val();
            if (!username) {
                $(uNameSelector).parent().parent().addClass('has-error');
                $(uNameSelector).focus().nextAll('.help-block').html('请输入用户名');
                return false;
            }
            if (!password) {
                $(pwdSelector).parent().parent().addClass('has-error');
                $(pwdSelector).focus().nextAll('.help-block').html('请输入密码');
                return false;
            }
            return true;
        },
        /**
         * 重置
         */
        formReset: function () {
            $('[class*=has-error]').removeClass('has-error');
            $('.help-block').html('');
        }
    };

    //===============帖子相关===================//
    $.Article = {
        post: function(articlePrefix){
            var btn = getEventTarget(arguments);
            var $btn = $(btn).button('loading');
            var content = $(articlePrefix + 'Text').code();
            var title = $(articlePrefix + 'Title').val();
            var catalog = $(articlePrefix + 'Catalog').val();
            var tags = $(articlePrefix + 'Tags').tagsinput('items');
            var article = {
                title: title,
                content: content,
                catalog: catalog,
                tags: tags
            };
            $.post('/article/post', article, function(data){
                var options = {
                    padding: 10,
                    title: '提示',
                    okValue: '确定',
                    ok: function(){
                        location.reload();
                    }
                };
                if(data.ret === 1){
                    options.content = '<font color="green">发表成功</font>';
                }else{
                    options.content = '<font color="red">发表失败</font>';
                }
                dialog(options).showModal();
            }).complete(function(){
                $btn.button('reset');
            });
        },
        /**
         * 重置
         */
        formReset: function (articlePrefix) {
            var content = $(articlePrefix + 'Text').code('');
            var title = $(articlePrefix + 'Title').val('');
            var catalog = $(articlePrefix + 'Catalog').val();
            var tags = $(articlePrefix + 'Tags').tagsinput('items');
        }
    };
});
